using OrchardCore.ContentManagement;
using System.Threading.Tasks;

namespace Lombiq.HelpfulLibraries.OrchardCore.Contents;

/// <summary>
/// 用于获取内容项或其特定版本的版本号的服务。
/// </summary>
public interface IContentVersionNumberService
{
    /// <summary>
    /// 返回内容项的当前最大版本号。
    /// </summary>
    Task<int> GetLatestVersionNumberAsync(string contentItemId);

    /// <summary>
    /// Returns the version number of a specific version.
    /// </summary>
    Task<int> GetCurrentVersionNumberAsync(string contentItemId, string contentItemVersionId);
}

public static class ContentVersionNumberServiceExtensions
{
    /// <summary>
    /// Returns the largest version number for the provided <paramref name="content"/>.
    /// </summary>
    public static Task<int> GetLatestVersionNumberAsync(
        this IContentVersionNumberService service,
        IContent content) =>
        service.GetLatestVersionNumberAsync(content?.ContentItem?.ContentItemId);

    /// <summary>
    /// Returns the current version number of the given <paramref name="content"/>.
    /// </summary>
    public static Task<int> GetCurrentVersionNumberAsync(
        this IContentVersionNumberService service,
        IContent content) =>
        service.GetCurrentVersionNumberAsync(
            content?.ContentItem?.ContentItemId,
            content?.ContentItem?.ContentItemVersionId);
}
